{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Exploring GPT-5: Comparing Verbosity Levels in AG2 AgentChat\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "This notebook demonstrates how to use different verbosity levels with GPT-5 in AG2's AgentChat framework.\n",
    "It compares the outputs and behaviors of agents when the verbosity setting is changed.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Key Feature: Verbosity Parameter\n",
    " \n",
    "The **verbosity** parameter allows you to control how detailed or concise the model's responses are, without needing to rewrite your prompts. This is especially useful for adapting the agent's communication style to different use cases."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "**Verbosity Levels Explained**\n",
    "\n",
    "The `verbosity` parameter controls how detailed the agent's responses are. It supports three levels:  \n",
    "\n",
    "1. **Low**: Short, minimal, and to-the-point responses.  \n",
    "   - *Best for*: Quick answers, concise summaries, UX scenarios\n",
    "\n",
    "2. **Medium** (default): Balanced detail. Provides enough information for most conversations and general use.  \n",
    "   - *Best for*: Everyday tasks, general conversations\n",
    "\n",
    "3. **High**: Very detailed, explanatory, and verbose replies.  \n",
    "   - *Best for*: Audits, teaching, documentation, handoffs\n",
    "\n",
    "> **Tip:**  \n",
    "> You don't need to rewrite your prompts—just set the `verbosity` parameter to adjust the response style as needed!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "from autogen import ConversableAgent, LLMConfig\n",
    "from autogen.agentchat import initiate_group_chat\n",
    "from autogen.agentchat.group.patterns import AutoPattern\n",
    "\n",
    "load_dotenv()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "### Agent Setup Instructions\n",
    "\n",
    "1. **Install Required Packages**:\n",
    "    Make sure you have installed the required packages:\n",
    "    ```\n",
    "    pip install ag2\n",
    "    ```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "2. **Set Up Environment Variables**:\n",
    "    Create a `.env` file in your project directory and add your OpenAI API key:\n",
    "    ```\n",
    "    OPENAI_API_KEY=your_openai_api_key_here\n",
    "    ```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "3. **Configure LLM and Agents**:\n",
    "  - The notebook demonstrates how to configure the `LLMConfig` for GPT-5 with different verbosity levels.\n",
    "  - It sets up three agents:\n",
    "    - `code_agent`: Codes the given task.\n",
    "    - `reviewer_agent`: Reviews the code.\n",
    "    - `user`: Initiates the conversation."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "4. **Run the Notebook**:\n",
    "  Execute the cells in order to see how agent behavior changes with different verbosity settings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = LLMConfig(\n",
    "    config_list={\n",
    "        \"model\": \"gpt-5-nano\",\n",
    "        \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n",
    "        \"api_type\": \"openai\",\n",
    "        \"verbosity\": \"low\",\n",
    "    }\n",
    ")\n",
    "\n",
    "code_agent = ConversableAgent(\n",
    "    name=\"code_agent\",\n",
    "    system_message=\"you are a coding agent. you need to code the task given to you\",\n",
    "    max_consecutive_auto_reply=5,\n",
    "    human_input_mode=\"NEVER\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "reviewer_agent = ConversableAgent(\n",
    "    name=\"reviewer_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    system_message=\"you are a reviewer agent. you need to review the code given to you\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "user = ConversableAgent(\n",
    "    name=\"user\",\n",
    "    human_input_mode=\"ALWAYS\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = AutoPattern(\n",
    "    initial_agent=code_agent,\n",
    "    agents=[code_agent, reviewer_agent],\n",
    "    user_agent=user,\n",
    "    group_manager_args={\"llm_config\": llm_config},\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "### Solving the 2 sums problem using the verbosity='low' verbosity setting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "result, context_variables, last_agent = initiate_group_chat(\n",
    "    pattern=pattern,\n",
    "    messages=\"write a python code to solve the 2 sums problem\",\n",
    "    max_rounds=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"LOW Code:\")\n",
    "print(result.chat_history[-2][\"content\"])\n",
    "print(\"*\" * 100)\n",
    "print(\"Reviewer:\")\n",
    "print(f\"Result: {result.summary}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "### Example: Using 'medium' verbosity setting\n",
    " In this example, we set the verbosity parameter to 'medium' when configuring the LLM.\n",
    " This instructs the model to provide responses that are more detailed than the default,\n",
    " but not as exhaustive as the 'high' verbosity setting. The model will include some\n",
    " explanations and reasoning in its answers, offering a balance between conciseness and detail."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = LLMConfig(\n",
    "    config_list={\n",
    "        \"model\": \"gpt-5\",\n",
    "        \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n",
    "        \"api_type\": \"openai\",\n",
    "        \"verbosity\": \"medium\",\n",
    "    }\n",
    ")\n",
    "\n",
    "code_agent = ConversableAgent(\n",
    "    name=\"code_agent\",\n",
    "    system_message=\"you are a coding agent. you need to code the task given to you\",\n",
    "    max_consecutive_auto_reply=5,\n",
    "    human_input_mode=\"NEVER\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "reviewer_agent = ConversableAgent(\n",
    "    name=\"reviewer_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    system_message=\"you are a reviewer agent. you need to review the code given to you\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "pattern = AutoPattern(\n",
    "    initial_agent=code_agent,\n",
    "    agents=[code_agent, reviewer_agent],\n",
    "    user_agent=user,\n",
    "    group_manager_args={\"llm_config\": llm_config},\n",
    ")\n",
    "\n",
    "result, context_variables, last_agent = initiate_group_chat(\n",
    "    pattern=pattern,\n",
    "    messages=\"write a python code to solve the 2 sums problem\",\n",
    "    max_rounds=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Medium Code:\")\n",
    "print(result.chat_history[-2][\"content\"])\n",
    "print(\"*\" * 100)\n",
    "print(\"Reviewer:\")\n",
    "print(f\"Result: {result.summary}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "### Example: Using 'high' verbosity setting\n",
    " This setting instructs the model to provide more detailed and comprehensive responses,\n",
    " In the following example, we demonstrate how to use the 'high' verbosity setting with GPT-5.\n",
    " including step-by-step reasoning, explanations, and additional context where applicable.\n",
    " Using higher verbosity can be especially useful when you want the model to elaborate on its answers,\n",
    " justify its choices, or provide more insight into its problem-solving process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = LLMConfig(\n",
    "    config_list={\n",
    "        \"model\": \"gpt-5\",\n",
    "        \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n",
    "        \"api_type\": \"openai\",\n",
    "        \"verbosity\": \"high\",\n",
    "    }\n",
    ")\n",
    "\n",
    "code_agent = ConversableAgent(\n",
    "    name=\"code_agent\",\n",
    "    system_message=\"you are a coding agent. you need to code the task given to you\",\n",
    "    max_consecutive_auto_reply=5,\n",
    "    human_input_mode=\"NEVER\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "reviewer_agent = ConversableAgent(\n",
    "    name=\"reviewer_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    system_message=\"you are a reviewer agent. you need to review the code given to you\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "pattern = AutoPattern(\n",
    "    initial_agent=code_agent,\n",
    "    agents=[code_agent, reviewer_agent],\n",
    "    user_agent=user,\n",
    "    group_manager_args={\"llm_config\": llm_config},\n",
    ")\n",
    "\n",
    "result, context_variables, last_agent = initiate_group_chat(\n",
    "    pattern=pattern,\n",
    "    messages=\"write a python code to solve the 2 sums problem\",\n",
    "    max_rounds=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"High Code:\")\n",
    "print(result.chat_history[-2][\"content\"])\n",
    "print(\"*\" * 100)\n",
    "print(\"Reviewer:\")\n",
    "print(f\"Result: {result.summary}\")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "GPT5 verbosity example",
   "tags": [
    "gpt5",
    "verbosity"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
